<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class ManageUser extends CI_Controller 
{

	public function index()
	{
		$data['employeData']=$this->employeData();
		$this->load->view('pruebas/manageuser_view',$data);

	}
	private function employeData()
	{
		$this->load->database();
		
		$this->db->select('*');
		$this->db->from('phone');
		$this->db->join('employe', 'phone.idphone = employe.idphone');
		$query=$this->db->get();
		
		$this->db->close();
		return $query;
	}
	public function update()
	{
		if(!$this->input->post('idemploye', TRUE) || !$this->input->post('value', TRUE)||!$this->input->post('field', TRUE))
		{
			echo "Datos incorrectos";
			return;
		}
	
		if($this->input->post('field', TRUE)=="Nombre del trabajador")
		{
			$data=array(
	               'name' => $this->input->post('value', TRUE),
	            );
		}
		else if($this->input->post('field', TRUE)=="Id del telefono")
		{
			$data=array(
					'idphone' => $this->input->post('value', TRUE),
			);
			
		}
		$this->load->database();
		
		$this->db->where('idemploye', $this->input->post('idemploye', TRUE));
		$this->db->update('employe', $data);
		
		$this->db->close();
		echo "Se han modificado los datos";
	}
	public function create()
	{
		if(!$this->input->post('idphone', TRUE) || !$this->input->post('name', TRUE))
		{
			echo "Datos incorrectos";
			return;
		}
		$data=array(
					'idphone'=>$this->input->post('idphone', TRUE),
					'name'=>$this->input->post('name', TRUE)
				);
		$this->load->database();
		
		$this->db->insert('employe', $data);
		$iid=$this->db->insert_id();
		$this->db->close();
		
		echo $iid;
		
	}
	public function delete()
	{
		if(!$this->input->post('idemploye', TRUE))
		{
			echo "Datos incorrectos";
			return;
		}
		$data=array(
				'idemploye'=>$this->input->post('idemploye', TRUE)
		);
		$this->load->database();
	
		$this->db->delete('employe', $data); 
		$this->db->close();
		
		echo "Usuario eliminado";
	
	}

}
?>